package com.example.liunanxin.lnx226ordering.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.beans.RegisterBean;
import com.example.liunanxin.lnx226ordering.iface.RegisterListener;
import com.example.liunanxin.lnx226ordering.model.RegisterModel;




public class RegisterActivity extends AppCompatActivity {


    private EditText etname, etpass, etpass2, etphone, etaddress,etcomment;
    private Button registerbtn,newregisterbtn;
    private Context context;
    private ImageView back;
    RegisterListener registerListener=new RegisterListener() {
        @Override
        public void onResponse(RegisterBean regInfo) {
            if (regInfo.getSuccess().equals("1")){
               Intent intent=new Intent();
                intent.putExtra("user",etname.getText().toString());
                setResult(RESULT_OK,intent);

                finish();
                Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(context,""+mag,Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        initView();
        initEvents();

    }

    private void initView() {

        etname = findViewById(R.id.editText2);
       etpass = findViewById(R.id.editText7);
      etpass2 = findViewById(R.id.editText9);
       etphone = findViewById(R.id.editText8);
        etaddress = findViewById(R.id.editText5);
        registerbtn = findViewById(R.id.button2);
        etcomment = findViewById(R.id.editText6);
        back=(ImageView)findViewById(R.id.imageView);
    }


    private void initEvents() {
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = etname.getText().toString();
                String userpass = etpass.getText().toString();
                Log.i("mag", userpass);
                String userpass1 = etpass2.getText().toString();
                Log.i("mag", userpass1);
                if (!userpass.equals(userpass1)) {
                    etpass2.setError("密码不一致");
                    return;
                }
                String mobilenum = etphone.getText().toString();
                String address = etaddress.getText().toString();
                String comment = etcomment.getText().toString();

                RegisterModel model = new RegisterModel();
                model.getRegisterResult(username, userpass,address,mobilenum,registerListener);
                finish();
            }
        });
//        back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
//                startActivity(intent);
//                finish();
//            }
//        });
    }

}
